/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.StringRes;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.EditText;

import com.ouertech.android.agm.lib.base.future.core.AgnettyFuture;
import com.ouertech.android.agm.lib.base.future.core.AgnettyResult;
import com.ouertech.android.agm.lib.base.utils.UtilList;
import com.ouertech.android.agm.lib.base.utils.UtilLog;
import com.ouertech.android.agm.lib.base.utils.UtilString;
import com.ouertech.android.agm.lib.ui.base.BaseUIFragment;
import com.ouertech.android.agm.lib.ui.base.FragmentTab;
import com.ouertech.android.agm.lib.ui.base.FragmentTabAdapter;
import com.ouertech.android.agm.lib.ui.base.defaults.fragment.BaseTopFragment;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Address;
import com.simon.mengkou.data.bean.base.Category;
import com.simon.mengkou.data.enums.EDirection;
import com.simon.mengkou.future.base.OuerFutureListener;
import com.simon.mengkou.system.constant.CstOuer;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.ui.view.CustomViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/2/29.
 * desc    : 购买界面
 */
public class BuyFragment extends BaseTopFragment{
    //tablayout
    @Bind(R.id.base_id_appbar_tabs)
    TabLayout mTabLayout;
    //viewpager
    @Bind(R.id.base_id_content_viewpager)
    ViewPager mVpPager;

    //与appbar tab关联的fragments
    private List<FragmentTab> mAppTabs;

    @Override
    protected void initTop() {
        setTitle(R.string.buy_title);
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.fragment_buy);
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this, getView());

        setOnRetryListener(new OnRetryListener() {
            @Override
            public void onRetry() {
                getCategory();
            }
        });

        //获取分类
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                getCategory();
            }
        }, 500);
    }

    /**
     * 获取购买分类
     */
    private void getCategory() {
        AgnettyFuture future = OuerApplication.mOuerFuture.buyCategory(
                new OuerFutureListener(mActivity) {
                    @Override
                    public void onStart(AgnettyResult result) {
                        super.onStart(result);
                        setLoading(true);
                    }

                    @Override
                    public void onComplete(AgnettyResult result) {
                        super.onComplete(result);
                        setLoading(false);

                        addAppTab(R.string.buy_recommend_title, CategoryFragment.class);

                        List<Category> categories = (List<Category>) result.getAttach();

                        if(UtilList.isNotEmpty(categories)) {
                            for(Category category : categories) {
                                Bundle bundle = new Bundle();
                                bundle.putString(CstOuer.KEY.ID, category.getId());
                                addAppTab(category.getName(), ProductListFragment.class, bundle);
                            }
                        }

                        mTabLayout.setVisibility(View.VISIBLE);
                        mVpPager.setAdapter(new FragmentTabAdapter(getChildFragmentManager(),
                                mAppTabs));
                        mTabLayout.setupWithViewPager(mVpPager);
                    }

                    @Override
                    public void onException(AgnettyResult result) {
                        super.onException(result);
                        setRetry(true);
                    }

                    @Override
                    public void onNetUnavaiable(AgnettyResult result) {
                        super.onNetUnavaiable(result);
                        setRetry(true);
                    }
                }
        );

        attachDestroyFutures(future);
    }


    /**
     * 添加appbar上的文本tab
     * @param txtRes
     * @param cls
     */
    public void addAppTab(@StringRes int txtRes, Class<? extends BaseUIFragment> cls) {
        addAppTab(getString(txtRes), cls, null);
    }

    /**
     * 添加appbar上的文本tab
     * @param title
     * @param cls
     */
    public void addAppTab(String title, Class<? extends BaseUIFragment> cls) {
        addAppTab(title, cls, null);
    }

    /**
     * 添加appbar上的文本tab
     * @param txtRes
     * @param cls
     * @param args
     */
    public void addAppTab(@StringRes int txtRes, Class<? extends BaseUIFragment> cls, Bundle args) {
        addAppTab(getString(txtRes), cls, args);
    }

    /**
     * 添加appbar上的文本tab
     * @param title
     * @param cls
     * @param args
     */
    public void addAppTab(String title, Class<? extends BaseUIFragment> cls, Bundle args) {
        if(mTabLayout != null && UtilString.isNotBlank(title)) {
            BaseUIFragment fragment = (BaseUIFragment) Fragment.instantiate(mActivity, cls.getName(), args);
            if(mAppTabs == null) {
                mAppTabs = new ArrayList<>(4);
            }

            FragmentTab tab = new FragmentTab();
            tab.setTitle(title);
            tab.setFragment(fragment);
            mAppTabs.add(tab);
        }
    }
}
